<?php

namespace app\index\controller;

use app\index\model\Minimodel;
use think\Controller;
use think\Db;
use app\index\controller\Basecontroller;

class Miniprogram extends Basecontroller
{
  function __construct()
  {
    parent::__construct();
    $this->model =  new Minimodel();
  }


  function getSwiper()
  {
    $res = $this->model->getSwiper();



    return res(1000, 'ok', $res);
  }

  function getCategroy()
  {
    $res = $this->model->getCategroy();

    return res(1000, 'ok', $res);
  }

  function getGoodsDetail()
  {
    $gid = $_GET['gid'];

    $res =  $this->model->getGoodsDetail($gid);

    return res(1000, 'ok', $res);
  }

  function homeGetGoods()
  {
    return res(1000, 'ok', $this->model->homeGetGoods());
  }
  function getGoodsItem()
  {
    $gid = $_GET['gid'];
    return res(1000, 'ok', $this->model->getGoodsItem($gid));
  }
  function userLogin()
  {

    $post = getPost();

    $res = $this->model->userLogin($post['openid']);
    return res(1000, 'ok', $res);
  }

  function checkOrder()
  {
    $post = getPost();
    if (empty($post['openid'])) {
      return res(1100, '服务器繁忙');
    }

    $isAmple = $this->model->checkMoeny($post['openid'],$post['allPrice']);
    if (!$isAmple) {
      return res(1001, '余额不足');
    }
    // 增加一条订单信息，并扣除余额
    $post['uid'] = $isAmple['uid'];


    $res = $this->model->insetOrder($post);
    // 扣除余额
    if ( $res === 1) {
      $res =  $this->model->demoney($isAmple);

      return res(1000,'ok', $res);
      
    }


    return res(1002, 'ok', $res);
  }

  // 微信用户 通过openid获得订单信息
  function getOrder(){
    $post = getPost();
    $res = $this->model->getOrder($post['openid']);

    return res(1000,'ok',$res);

  } 


  function checkGoods()
  {
    $post = getPost();

    $res= $this->model->updateOrder($post['oid'],3);
    if ($res) {
      # code...
      return res(1000,'ok');

    }

    return res(1001,'服务器繁忙！');
  }

  function payloadOrder(){
    $post = getPost();
    if (empty($post['openid'])) {
      return res(1100, '服务器繁忙');
    }
    $isAmple = $this->model->checkMoeny($post['openid'],$post['payNum']);

    if (!$isAmple) {
      return res(1001, '余额不足');
    }

    // 更新订单信息为待收货的
    $res =$this->model->updateOrder($post['oid'],2 );
  
    if ($res) {
      // 如果成功则进行扣钱
      $this->model->demoney($isAmple);

      return res(1000,'付款成功'); 
    }
    return  res(1003,'服务器繁忙',$res);
  }
  // 增加用户浏览历史记录
  function addHistory(){
    $post = getPost();

    if (empty($post['openid']) || empty($post['gid'])) {
      return res(1100, '服务器繁忙');
    }

    $res =$this->model->addHistory($post['openid'] , $post['gid']);

    return res(1000, 'ok' , $res);

  }
  
  function collectGoods(){
    $post = getPost();
    $res =$this->model->collectGoods($post['openid'] , $post['gid'] , $post['collect'] );
    return res(1000, 'ok' , $res);

  }
  function getCollectGoods(){
    if (empty($_GET['openid'])){
      return res(1100, '服务器繁忙');
    }
    $res = $this->model->getCollectGoods($_GET['openid']);
    return res(1000, 'ok' , $res);
  }

  // 小程商品搜索
  function goodsQuery(){
    

    $key = trim($_GET['query']);
    $res =  $this->model->goodsQuery($key);

    return res(1000, 'ok' ,  $res);

    
  }

}
